package com.bh.controller;

import com.alibaba.fastjson.JSON;
import com.bh.pojo.entry.Menu;
import com.bh.pojo.entry.Role;
import com.bh.service.MenuService;
import com.bh.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private MenuService menuService;


    @RequestMapping("/findAll")
    public String findAll(
            @RequestParam(required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(required = false,defaultValue = "1")Integer pageSize,Model model,String  likename){
        PageHelper.startPage(pageNum,pageSize);
        List<Role> roleList=roleService.findAll(likename);
        PageInfo<Role> roleInfo = new PageInfo<>(roleList);

        model.addAttribute("roleInfo",roleInfo);
        model.addAttribute("likename",likename);
        return "role_list";
    }

    @RequestMapping("/toAllocationMenu")
    public String toAllocationMenu(Integer id,Model model){
        //查询所有的菜单信息
        List<Menu> menuList = menuService.findAll(null);
        //根据role的id查询菜单
        List<Menu> roleMenu= menuService.findById(id);
        model.addAttribute("menuList", JSON.toJSONString(menuList));
        model.addAttribute("roleMenu", JSON.toJSONString(roleMenu));
        model.addAttribute("rid",id);
        return "role_allocationMenu";
    }

    @RequestMapping("allocationMenu")
    public String allocationMenu (@RequestParam Integer rid,@RequestParam List<Integer> mids){
        roleService.allocationMenu(rid,mids);
    return "redirect:/role/findAll.do";
    }


}
